<template>
  <div>
    <div class="zuipagesubWrap" :style="{'minHeight': getlistHeight}">
      <div class="zcontent">
        <div class="zui-flex ztableWrap">
          <div class="J_left">
            <div class="zui-flex J_row">
              <div class="J_col w2">医院
              </div>
            </div>
            <div class="zui-flex J_row" v-for="(item, index) in tableData" :key="index">
              <div class="J_col w2">{{item.hospital}}</div>
            </div>
            <div class="zui-flex J_row">
              <div class="J_col w2">总计
              </div>
            </div>
          </div>
          <cube-scroll
            ref="scroll"
            :data="tableData"
            direction="horizontal"
            class="horizontal-scroll-list-wrap">
            <div class="J_right">
              <div class="zui-flex J_row">
                <div class="J_col w2">物料号</div>
                <div class="J_col w2">名称</div>
                <div class="J_col w2">上月预测</div>
                <div class="J_col w2">上月实际</div>
                <div class="J_col w2">准确度</div>

              </div>
              <div class="zui-flex J_row" v-for="(item, index) in tableData" :key="index">
                <div class="J_col w2">{{item.code}}</div>
                <div class="J_col w2">{{item.name}}</div>
                <div class="J_col w2">{{item.lastMonthForecast}}</div>
                <div class="J_col w2">{{item.lastMonthActual}}</div>
                <div class="J_col w2" :class="retNewClass(item.accuracy)">{{item.accuracy}}</div>

              </div>
              <div class="zui-flex J_row">
                <div class="J_col w2"></div>
                <div class="J_col w2"></div>
                <div class="J_col w2">{{lastMonthForecastTotal}}</div>
                <div class="J_col w2">{{lastMonthActualTotal}}</div>
                <div class="J_col w2" :class="retNewClass(accuracyTotal)">{{accuracyTotal}}</div>

              </div>
            </div>
          </cube-scroll>
        </div>
      </div>
    </div>

    <cube-view></cube-view>
  </div>
</template>

<script>
import {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} from 'vuex'

import BusFactory from 'vue-happy-bus';
import CubePage from '@/components/cube-page'
import CubeView from '@/components/cube-view'
import Drawer from '@/components/drawer'
import {zHistoryList} from '@/util/api';

export default {
  name: 'HistoryList',
  components: {
    CubeView,
    CubePage,
    Drawer
  },
  computed: {
    ...mapState(['loginUserInfo']),
  },
  data () {
    return {
      bus: BusFactory(this),
      showLoading: true,
      tableData: [],
      total: 0,
      start: 1,
      pageSize: 9999,
      pullDownRefreshThreshold: 60,
      pullDownRefreshStop: 40,
      options: {
        pullDownRefresh: {
          threshold: 60,
          txt: '正在刷新...'
        },
        pullUpLoad: {
          threshold: 60,
          txt: {
            more: '上拉加载更多',
            noMore: '没有更多数据了...'
          }
        },
        scrollbar: true,
        click: true,
        tap: true,
        taps: true,
      },
      getlistHeight: this.zutil.getlistHeight(88),
      searchOut: {},
      drawerShow: false,
      hospitalList: [],
      datePicker: null,
      lastMonthForecastTotal: '',
      lastMonthActualTotal: '',
      accuracyTotal: '',
      accuracySTotal: '',
    }
  },
  created () {

  },
  mounted() {
    this.bus.$on('pageRefresh', (searchOut) => {
      this.zutil.deepClone(this.searchOut, searchOut);
      this.resetPage();
      this.searchData();
    })
    this.fetchList();
  },
  watch: {

  },
  methods: {
    resetPage () {
      this.tableData = [];
      this.total = 0;
      this.start = 1;
    },
    onPullingDown() {
      // 下拉刷新
      this.resetPage();
      this.fetchList();
    },
    onPullingUp() {
      // 上拉加载更多
      if(this.total && this.tableData.length < this.total){
        this.fetchList();
      }else if(this.total && this.tableData.length == this.total){
        this.$refs.scroll.forceUpdate();
        return;
      }
    },
    searchData () {
      this.start = 1;
      this.fetchList();
    },
    async fetchList () {
      let tbl_param = {
        page: this.start,
        pagesize: this.pageSize,
        
      }
      this.zutil.deepClone(tbl_param, this.searchOut);

      const zuiCubeLoading = this.$createToast({time: 0,txt: '正在加载中...'});
      zuiCubeLoading.show();
      let res = await zHistoryList(tbl_param);
      res = this.zutil.formatAxiosData(res);
      zuiCubeLoading.hide();

      if(res.code == '0000'){
        if(res.data.list && res.data.list.length){
          this.tableData = this.tableData.concat(res.data.list);
          this.total = res.data.total;
          this.start++;
          this.lastMonthForecastTotal = res.data.lastMonthForecastTotal;
          this.lastMonthActualTotal = res.data.lastMonthActualTotal;
          this.accuracyTotal = res.data.accuracyTotal;
          this.accuracySTotal = res.data.accuracySTotal;
        }else{
          this.$refs.scroll.forceUpdate();
        }
      }else{
        this.$createToast({type: 'error', time: 2000, txt: res.message || "获取历史预测列表失败！"}).show();
        this.$refs.scroll.forceUpdate();
      }
      // 如果微信确实无法触发点击事件，再放开这段代码
      // this.zutil.initBtnsClickEvent(this, '.zuiReplayBtn', this.zreplay);
    },
    retNewClass (val) {
      let _val = parseFloat(val, 10);
      let _cls = 'zui_done';
      if(_val < 50){
        _cls = 'zui_new';
      }else if(_val < 80){
        _cls = 'zui_undo';
      }
      return _cls;
    }
  }
}
</script>

<style scoped lang="scss">

.scroll-list-wrap {
  border-radius: 0;
}
.scroll-list-wrap .zcardWrap .zcard .zcardRow .zlabel {
  width: 120px;
}

</style>
